package com.time.admin.domain.service.impl;

import com.time.admin.common.enums.IsDeletedFlagEnum;
import com.time.admin.common.utils.CollectionUtils;
import com.time.admin.domain.constants.ExceptionCode;
import com.time.admin.domain.converter.CategoryBOConverter;
import com.time.admin.domain.converter.PostCategoryBOConverter;
import com.time.admin.domain.entity.CategoryBO;
import com.time.admin.domain.entity.PostCategoryBO;
import com.time.admin.domain.exception.AlreadyExistsException;
import com.time.admin.domain.exception.NotFoundDomainException;
import com.time.admin.domain.service.AdminPostCategoryDomainService;
import com.time.admin.infra.basic.entity.Category;
import com.time.admin.infra.basic.entity.PostCategory;
import com.time.admin.infra.basic.service.CategoryService;
import com.time.admin.infra.basic.service.PostCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 文章分类关联管理领域服务层接口实现
 *
 * @author: HuangQi
 * @date: Created in 11:34 2025/10/15
 * @description: 文章分类关联管理领域服务层接口实现类
 */
@Slf4j
@Service("adminPostCategoryService")
public class AdminPostCategoryDomainServiceImpl implements AdminPostCategoryDomainService {

    @Autowired
    private PostCategoryService postCategoryService;

    @Autowired
    private CategoryService categoryService;


    @Override
    public Boolean add(PostCategoryBO postCategoryBO) {
        if (log.isInfoEnabled()) {
            log.info("AdminPostCategoryDomainServiceImpl.add.bo:{}", postCategoryBO);
        }

        // 参数转换
        PostCategory postCategory = PostCategoryBOConverter.INSTANCE.convertBOToEntity(postCategoryBO);
        postCategory.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

        // 检查分类是否存在
        if (Objects.isNull(categoryService.queryById(postCategory.getCategoryId()))) {
            throw new NotFoundDomainException(ExceptionCode.CATEGORY_NOT_EXISTS, "分类ID不存在");
        }

        // 检查关联关系是否已存在
        List<PostCategory> postCategoryList = postCategoryService.queryByCondition(postCategory);
        if (!CollectionUtils.isEmpty(postCategoryList)) {
            throw new AlreadyExistsException(ExceptionCode.POST_CATEGORY_RELATION_EXISTS, "文章分类关联关系已存在");
        }

        // 添加文章分类关联关系
        int count = postCategoryService.insert(postCategory);

        // 返回结果
        return count > 0;
    }

    @Override
    public Boolean update(PostCategoryBO postCategoryBO) {
        if (log.isInfoEnabled()) {
            log.info("AdminPostCategoryDomainServiceImpl.update.bo:{}", postCategoryBO);
        }

        // 参数转换
        PostCategory postCategory = PostCategoryBOConverter.INSTANCE.convertBOToEntity(postCategoryBO);

        // 更新文章分类关联关系
        int count = postCategoryService.update(postCategory);

        // 返回结果
        return count > 0;
    }

    @Override
    public Boolean delete(PostCategoryBO postCategoryBO) {
        if (log.isInfoEnabled()) {
            log.info("AdminPostCategoryDomainServiceImpl.delete.bo:{}", postCategoryBO);
        }

        // 参数转换
        PostCategory postCategory = PostCategoryBOConverter.INSTANCE.convertBOToEntity(postCategoryBO);
        postCategory.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());

        // 更新文章分类关联关系
        int count = postCategoryService.update(postCategory);

        // 返回结果
        return count > 0;
    }

    @Override
    public CategoryBO queryCategory(PostCategoryBO postCategoryBO) {
        if (log.isInfoEnabled()) {
            log.info("AdminPostCategoryDomainServiceImpl.queryCategory.bo:{}", postCategoryBO);
        }

        // 参数转换
        PostCategory postCategory = PostCategoryBOConverter.INSTANCE.convertBOToEntity(postCategoryBO);
        postCategory.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

        // 根据文章id查询关联的分类id
        PostCategory queryPostCategory = postCategoryService.queryByPostId(postCategory.getPostId());
        if (Objects.isNull(queryPostCategory) || queryPostCategory.getCategoryId() == null) {
            throw new NotFoundDomainException(ExceptionCode.POST_NOT_LINKED_CATEGORY, "文章未关联分类");
        }

        // 根据分类id查询分类信息
        Category category = categoryService.queryById(queryPostCategory.getCategoryId());
        CategoryBO categoryBO = CategoryBOConverter.INSTANCE.convertEntityToBO(category);

        // 返回结果
        return categoryBO;
    }
}
